/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012-2013 Oliver Katz */

#include "error.h"

namespace sleepyface
{
    namespace opal
    {
        unsigned int nerrors = 0;
        stringstream errorss;

        void error(Token t, string file, string msg)
        {
            if (!file.empty())
            {
        	   errorss << "- " << file << "@" << t.getLine() << ":" << t.getColumn() << " - " << msg << "\n";
        	   nerrors++;
            }
        }

        void note(string dsc, string val, string file)
        {
            if (!file.empty())
            {
                errorss << "    - " << dsc << " <= \"" << val << "\"\n";
            }
        }

        void note(string dsc, int val, string file)
        {
            if (!file.empty())
            {
                errorss << "    - " << dsc << " <= " << val << "\n";
            }
        }

        void note(string dsc, string file)
        {
            if (!file.empty())
            {
                errorss << "    - " << dsc << "\n";
            }
        }

        bool dumpErrors()
        {
            if (nerrors == 1)
                cout << "there was an error\n";
            else if (nerrors > 1)
                cout << "there were " << nerrors << " errors\n";
            cout << errorss.str();

            bool rtn = (nerrors != 0);

            nerrors = 0;
            errorss.str("");

            return rtn;
        }

        void exitIfErrors()
        {
            if (dumpErrors())
        		_exit(1);
        }
    }
}
